package de.medieninf.sensimg;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;

/**
 * User interface for all settings.
 * @author pb
 */
public class SettingsForm extends Form {
    /**
     * Maximal URL length.
     */
    private static final int MAX_URL_LENGTH = 80;
    /**
     * Local cache of settings singleton.
     */
    private Settings settings;
    /**
     * Select where to send data to.
     */
    private ChoiceGroup sendUrlChoice;
    /**
     * Edit or display selected send URL.
     */
    private TextField sendUrlEntry;
    /**
     * Select where to retrieve images from.
     */
    private ChoiceGroup recvUrlChoice;
    /**
     * Edit or display selected retrieval URL.
     */
    private TextField recvUrlEntry;
    /**
     * Command to save changes.
     */
    private Command cmdSave = new Command("Save", Command.OK, 1);
    /**
     * CommandListener of constructing MIDlet.
     */
    private CommandListener upperCl;
    /**
     * Local CommandListener.
     */
    private CommandListener cl = new CommandListener() {
        public void commandAction(final Command c, final Displayable d) {
            if (c == cmdSave) {
                icl.commandAction(cmdAdd, sendUrlEntry);
                settings.setCurrentSendUrl(sendUrlEntry.getString());
                icl.commandAction(cmdAdd, recvUrlEntry);
                settings.setCurrentRecvUrl(recvUrlEntry.getString());
            }
            upperCl.commandAction(c, d);
        }
    };
    /**
     * Command to add an URL option.
     */
    private Command cmdAdd = new Command("Add", Command.ITEM, 2);
    /**
     * Handle the intent to add an URL option.
     */
    private ItemCommandListener icl = new ItemCommandListener() {
        public void commandAction(final Command c, final Item item) {
            if (c == cmdAdd) {
                if (item == sendUrlEntry) {
                    String entry = sendUrlEntry.getString();
                    if (settings.addSendUrl(entry)) {
                        int index = sendUrlChoice.append(entry, null);
                        sendUrlChoice.setSelectedIndex(index, true);
                    }
                } else if (item == recvUrlEntry) {
                    String entry = recvUrlEntry.getString();
                    if (settings.addRecvUrl(entry)) {
                        int index = recvUrlChoice.append(entry, null);
                        recvUrlChoice.setSelectedIndex(index, true);
                    }
                }
            }
        }
    };
    /**
     * Handle the intent to select an URL selection.
     */
    private ItemStateListener isl = new ItemStateListener() {
        public void itemStateChanged(final Item item) {
            String msg;
            if (item == sendUrlChoice) {
                msg = sendUrlChoice.getString(sendUrlChoice.getSelectedIndex());
                sendUrlEntry.setString(msg);
            } else if (item == recvUrlChoice) {
                msg = recvUrlChoice.getString(recvUrlChoice.getSelectedIndex());
                recvUrlEntry.setString(msg);
            }
        }
    };
    /**
     * Construct a SettingsForm instance.
     * @param pupperCl CommandListener from constructing place
     * @param cmds the commands to be displayed
     */
    public SettingsForm(final CommandListener pupperCl, final Command[] cmds) {
        super("Settings");
        this.upperCl = pupperCl;
        for (int i = 0; i < cmds.length; i++) {
            addCommand(cmds[i]);
        }
        addCommand(cmdSave);
        this.setCommandListener(cl);
        settings = Settings.getSettings();
        sendUrlChoice = new ChoiceGroup("SendUrl",
                                        ChoiceGroup.EXCLUSIVE,
                                        settings.getPossibleSendUrls(),
                                        null);
        append(sendUrlChoice);
        sendUrlEntry = new TextField("Send URL", "",
                                     MAX_URL_LENGTH, TextField.URL);
        sendUrlEntry.addCommand(cmdAdd);
        sendUrlEntry.setItemCommandListener(icl);
        append(sendUrlEntry);
        recvUrlChoice = new ChoiceGroup("RecvUrl",
                                        ChoiceGroup.EXCLUSIVE,
                                        settings.getPossibleRecvUrls(),
                                        null);
        append(recvUrlChoice);
        recvUrlEntry = new TextField("Recv URL", "",
                                     MAX_URL_LENGTH, TextField.URL);
        recvUrlEntry.addCommand(cmdAdd);
        recvUrlEntry.setItemCommandListener(icl);
        append(recvUrlEntry);
        isl.itemStateChanged(sendUrlChoice);
        isl.itemStateChanged(recvUrlChoice);
        setItemStateListener(isl);
        sendUrlChoice.setSelectedIndex(settings.getCurrentSendUrlIndex(),
                                       true);
        recvUrlChoice.setSelectedIndex(settings.getCurrentRecvUrlIndex(),
                                       true);
        String msg = sendUrlChoice.getString(sendUrlChoice.getSelectedIndex());
        sendUrlEntry.setString(msg);
        msg = recvUrlChoice.getString(recvUrlChoice.getSelectedIndex());
        recvUrlEntry.setString(msg);
    }
}
